package com.ruoyi.cat.service.impl;

import java.util.Collections;
import java.util.List;

import com.ruoyi.cat.domain.CatActivityTable;
import com.ruoyi.cat.domain.CatArticletaskTable;
import com.ruoyi.cat.domain.CatIprecordTable;
import com.ruoyi.cat.domain.DTO.ActivityDTO;
import com.ruoyi.cat.domain.DTO.TradeNumDTO;
import com.ruoyi.cat.domain.VO.*;
import com.ruoyi.cat.mapper.CatIprecordTableMapper;
import com.ruoyi.cat.service.ICatArticletaskTableService;
import com.ruoyi.cat.service.ICatIprecordTableService;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * IP记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-30
 */
@Service
@Slf4j
public class CatIprecordTableServiceImpl implements ICatIprecordTableService
{
    @Autowired
    private CatIprecordTableMapper catIprecordTableMapper;

    @Autowired
    private CatActivityTableServiceImpl catActivityTableService;

    @Autowired
    private ICatArticletaskTableService iCatArticletaskTableService;




    /**
     * 查询IP记录
     * 
     * @param id IP记录主键
     * @return IP记录
     */
    @Override
    public CatIprecordTable selectCatIprecordTableById(Long id)
    {
        return catIprecordTableMapper.selectCatIprecordTableById(id);
    }

    /**
     * 查询IP记录列表
     * 
     * @param catIprecordTable IP记录
     * @return IP记录
     */
    @Override
    public List<CatIprecordTable> selectCatIprecordTableList(CatIprecordTable catIprecordTable)
    {
        return catIprecordTableMapper.selectCatIprecordTableList(catIprecordTable);
    }

    /**
     * 新增IP记录
     * 
     * @param catIprecordTable IP记录
     * @return 结果
     */
    @Override
    public int insertCatIprecordTable(CatIprecordTable catIprecordTable)
    {
        catIprecordTable.setCreateTime(DateUtils.getNowDate());
        return catIprecordTableMapper.insertCatIprecordTable(catIprecordTable);
    }

    /**
     * 修改IP记录
     * 
     * @param catIprecordTable IP记录
     * @return 结果
     */
    @Override
    public int updateCatIprecordTable(CatIprecordTable catIprecordTable)
    {
        return catIprecordTableMapper.updateCatIprecordTable(catIprecordTable);
    }

    /**
     * 批量删除IP记录
     * 
     * @param ids 需要删除的IP记录主键
     * @return 结果
     */
    @Override
    public int deleteCatIprecordTableByIds(Long[] ids)
    {
        return catIprecordTableMapper.deleteCatIprecordTableByIds(ids);
    }

    /**
     * 删除IP记录信息
     * 
     * @param id IP记录主键
     * @return 结果
     */
    @Override
    public int deleteCatIprecordTableById(Long id)
    {
        return catIprecordTableMapper.deleteCatIprecordTableById(id);
    }

    @Override
    public CatIprecordTable getIprecordTable(CatIprecordTable catIprecordTable) {
        return catIprecordTableMapper.getIprecordTable(catIprecordTable);
    }

    @Override
    public CatIprecordTable getStaskData(CatIprecordTable catIprecordTable) {
        return catIprecordTableMapper.getStaskData(catIprecordTable);
    }

    @Override
    public CatIprecordTable getStaskDataNo(CatIprecordTable catIprecordTable) {
        return catIprecordTableMapper.getStaskDataNo(catIprecordTable);
    }

    @Override
    public CatIprecordTable getTotalUvCount(CatIprecordTable catIprecordTable) {
        return catIprecordTableMapper.getTotalUvCount(catIprecordTable);
    }

    @Override
    public List<CatIprecordTable> getShareUserId() {
        return catIprecordTableMapper.getShareUserId();
    }

    @Override
    public boolean  existsShareRecord(CatIprecordTable query) {
        return catIprecordTableMapper.countShareRecord(query)> 0;
    }

    @Override
    public Long getIprecordTableBy(DataScreeningVO query) {
        return catIprecordTableMapper.getIprecordTableBy(query);
    }

    @Override
    public Long getIprecordTableDay(DataScreeningVO query) {
        return catIprecordTableMapper.getIprecordTableDay(query);
    }

    @Override
    public Long getIprecordTableByNo(DataScreeningVO query) {
        return catIprecordTableMapper.getIprecordTableByNo(query);
    }

    @Override
    public Long getIprecordTableCount(DataScreeningVO query) {
        return catIprecordTableMapper.getIprecordTableCount(query);
    }

    @Override
    public TradeNumDTO getTradeNum(TradeNumVO tradeNumDTO) {
        return catIprecordTableMapper.getTradeNum(tradeNumDTO);
    }

    @Override
    public Integer insertCatIpCodeUnique(CatIpCodeVO catIpCodeVO) {
        return catIprecordTableMapper.insertCatIpCodeUnique(catIpCodeVO);
    }

    @Override
    public ActivityDTO getActivityOverview(DataScreeningVO dataquery) {
        log.info("----------- dataquery: {}",dataquery);
     //   dataquery.setType("2");
        ActivityDTO activityDTO =  catIprecordTableMapper.getIprecordCountRW(dataquery);
        if (activityDTO == null) {
            activityDTO = new ActivityDTO();
            activityDTO.setVisitsToday(0L);
            activityDTO.setVisitorCount(0L);
        }
        log.info("----------- dataquery111---: {}",dataquery);
        if (dataquery.getType().equals("1")){
            CatArticletaskTable catActivityTable = iCatArticletaskTableService.selectCatArticletaskTableById(dataquery.getActivityId());
            if (catActivityTable != null){
                activityDTO.setTotalVisits(catActivityTable.getQuantity());
                activityDTO.setForwardingFrequency(catActivityTable.getForwardNum());
                dataquery.setUserId(null);
                activityDTO.setTotalVisitorCount(catIprecordTableMapper.getIprecordTableBy(dataquery));

            }
        }else{
            //dataquery.setUserId(null);
            activityDTO.setTotalVisitorCount(catIprecordTableMapper.getIprecordTableBy(dataquery));
            //activityDTO.setTotalVisitorCount(catIprecordTableMapper.getIprecordTableBy(dataquery));
            CatForward catForward = new CatForward();
            catForward.setArticleId(dataquery.getActivityId());
            catForward.setUserId(dataquery.getUserId());
            CatForward forward = catIprecordTableMapper.selectArticleByUserId(catForward);
            if (forward != null) {
                activityDTO.setForwardingFrequency(forward.getActivityForwardNum());
            }else{
                catForward.setUserId(null);
                CatForward forward1 = catIprecordTableMapper.selectArticleByUserId(catForward);
                activityDTO.setForwardingFrequency(forward1.getActivityForwardNum());
            }
            ActivityDTO activityData =  catIprecordTableMapper.getIprecordCountCS(dataquery);
            if (activityData != null) {
                activityDTO.setTotalVisits(activityData.getVisitsToday());
            }
        }
        return activityDTO;
    }

    @Override
    public List<ChartVO> getActivityChart(DataScreeningVO dataquery) {
        return catIprecordTableMapper.getActivityChart(dataquery);
    }

    @Override
    public ActivityDTO getArticleOverview(DataScreeningVO dataquery) {
     //   dataquery.setType("1");
        ActivityDTO activityDTO =  catIprecordTableMapper.getIprecordCount(dataquery);
        if (activityDTO == null) {
            activityDTO = new ActivityDTO();
            activityDTO.setVisitsToday(0L);
            activityDTO.setVisitorCount(0L);
        }
     //   catActivityTableService.selectCatActivityTableByIdTask(dataquery.getActivityId());
        if (dataquery.getType().equals("1")){
            CatArticletaskTable catActivityTable = iCatArticletaskTableService.selectCatArticletaskTableById(dataquery.getActivityId());
            if (catActivityTable != null){
                dataquery.setUserId(null);
                activityDTO.setTotalVisits(catIprecordTableMapper.getIprecordTableBy(dataquery));
                activityDTO.setForwardingFrequency(catActivityTable.getForwardNum());
                activityDTO.setTotalVisitorCount(catIprecordTableMapper.getIprecordTableDay(dataquery));

            }
        }else{
            log.info("----------- dataquery111---: {}",dataquery);
            activityDTO.setTotalVisitorCount(catIprecordTableMapper.getIprecordTableDay(dataquery));
            CatForward catForward = new CatForward();
            catForward.setArticleId(dataquery.getActivityId());
            CatForward forward = catIprecordTableMapper.selectArticleByUserId(catForward);
            if (forward != null) {
                activityDTO.setForwardingFrequency(forward.getActivityForwardNum());
            }else{
                activityDTO.setForwardingFrequency(0L);
            }
            ActivityDTO activityData =  catIprecordTableMapper.getIprecordCountCS(dataquery);
            if (activityData != null) {
                activityDTO.setTotalVisits(activityData.getVisitsToday());
            }
        }

        return activityDTO;
    }

    @Override
    public List<ChartVO> getArticleChart(DataScreeningVO dataquery) {
        return catIprecordTableMapper.getActivityChart(dataquery);
    }

    @Override
    public Integer saveArticleData(CatForward catForward) {
        return catIprecordTableMapper.saveArticleData(catForward);
    }

    @Override
    public CatForward selectArticleByUserId(CatForward catForward) {
        return catIprecordTableMapper.selectArticleByUserId(catForward);
    }

    @Override
    public List<ChartVO> getActivityData(DataScreeningVO dataquery) {
        return catIprecordTableMapper.getActivityData(dataquery);
    }

    @Override
    public ActivityDTO getIprecordCountCS(DataScreeningVO query) {
        return catIprecordTableMapper.getIprecordCountCS(query);
    }

    @Override
    public Long countShareRecord(CatIprecordTable query) {
        return catIprecordTableMapper.countShareRecord(query);
    }
}
